﻿/* ===============================================================================  
 * Copyright (c) 2010 Acid Frameworks
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * 
 * =============================================================================== */

using System;
using System.Net.Sockets;

namespace Acid.Network.Udp
{
    /// <summary>
    /// UDP Broadcaster.
    /// </summary>
    public class Broadcaster : IDisposable
    {
        private UdpClient client;
       
        /// <summary>
        /// Creates a new UDP Broadcaster with a default SendBufferSize of 8192.
        /// </summary>
        /// <param name="hostname">The hostname address of where packets will be sent to.</param>
        /// <param name="port">The port that packets will use.</param>
        public Broadcaster(string hostname, int port) : this(hostname, port, 8192){}
       
        /// <summary>
        /// Creates a new UDP Broadcaster
        /// </summary>
        /// <param name="hostname">The hostname address of where packets will be sent to.</param>
        /// <param name="port">The port that packets will use.</param>
        /// <param name="sendBufferSize">The size of the send buffer</param>
        public Broadcaster(string hostname, int port, int sendBufferSize)
        {
            this.client = new UdpClient(hostname, port);

            this.client.Client.SendBufferSize = sendBufferSize;
        }

        #region Methods
        /// <summary>
        /// Sends the packet to its destination. Note, UDP does not garentee that the
        /// recipient will receive this packet. 
        /// </summary>
        /// <param name="packet"></param>
        public void Send(Packet packet)
        {
            byte [] data = Packet.Serialize(packet);

            this.client.Send(data, data.Length);
        }

        #endregion

        #region IDisposable Members

        private bool disposed = false;

        private void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (!disposed)
                {
                    this.client.Close();

                    disposed = true;
                }
            }
        }
        
        public void Dispose()
        {
            this.Dispose(true);
        }

        ~Broadcaster()
        {
            this.Dispose(false);
        }

        #endregion
    }
}
